/*
 * @Author: Joel
 * @Date: 2025-04-09 10:15:39
 * @LastEditors: Joel
 * @LastEditTime: 2025-05-25 18:38:02
 * @FilePath: /gd32f303cct6/usr/board_driver/button_input.c
 * @Description:
 * Copyright (C) 2025 YiJiaLink, All Rights Reserved.
 * 2025-04-09 10:15:39
 */
#include "button_input.h"
static void power_button_init(void);
static void key_up_down_init(void);
static void confirm_button_init(void);

struct usr_key device_button = {
.up_key = 1,
.power_key =1,
.down_key = 1,
.confirm_key = 1
};

void EXTI10_15_IRQHandler(void)
{
    struct usr_key *p_button = &device_button;

    if (RESET != exti_interrupt_flag_get(UP_KEY_EXTI_LINE))
    {
        p_button->up_key = gpio_input_bit_get(UP_KEY_GPIO_PORT,UP_KEY_PIN);
        exti_interrupt_flag_clear(UP_KEY_EXTI_LINE);
    }
}
void EXTI5_9_IRQHandler(void)
{
}
/*  电源键中断入口 */
void EXTI0_IRQHandler(void)
{
    struct usr_key *p_button = &device_button;

    if (RESET != exti_interrupt_flag_get(WAKEUP_KEY_EXTI_LINE))
    {
        p_button ->power_key = gpio_input_bit_get(WAKEUP_KEY_GPIO_PORT,WAKEUP_KEY_PIN);
        exti_interrupt_flag_clear(WAKEUP_KEY_EXTI_LINE);
    }
}
/*  方向下键中断入口 */
void EXTI2_IRQHandler(void)
{
    struct usr_key *p_button = &device_button;

    if (RESET != exti_interrupt_flag_get(DOWN_KEY_EXTI_LINE))
    {
        p_button ->down_key = gpio_input_bit_get(DOWN_KEY_GPIO_PORT,DOWN_KEY_PIN);
        exti_interrupt_flag_clear(DOWN_KEY_EXTI_LINE);
    }
}
/**
 * @func: 
 * @description: 获取全部按键标志位
 * @return {*}
 * @example: 
 */
struct usr_key *get_button_pressed(void)
{
    struct usr_key *p_button = &device_button;
    return p_button;
}
/**
 * @func: 
 * @description: 初始化所有按键
 * @return {*}
 * @example: 
 */
void button_all_init(void)
{
    power_button_init();
    key_up_down_init();
    confirm_button_init();

    return;
}
/**
 * @func: 
 * @description: 电源键初始化
 * @return {*}
 * @example: 
 */
static void power_button_init(void)
{

    /* enable the key clock */
    rcu_periph_clock_enable(WAKEUP_KEY_GPIO_CLK);
    rcu_periph_clock_enable(RCU_AF);

    /* configure button pin as input */
    gpio_init(WAKEUP_KEY_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, WAKEUP_KEY_PIN);

    /* enable and set key EXTI interrupt to the lowest priority */
    nvic_irq_enable(WAKEUP_KEY_EXTI_IRQn, 2U, 0U);

    /* connect key EXTI line to key GPIO pin */
    gpio_exti_source_select(WAKEUP_KEY_EXTI_PORT_SOURCE, WAKEUP_KEY_EXTI_PIN_SOURCE);

    /* configure key EXTI line */
    exti_init(WAKEUP_KEY_EXTI_LINE, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(WAKEUP_KEY_EXTI_LINE);

    return;
}
/**
 * @func: 
 * @description: 功能键初始化
 * @return {*}
 * @example: 
 */
static void key_up_down_init(void)
{
    /* UP */
    /* enable the key clock */
    rcu_periph_clock_enable(UP_KEY_GPIO_CLK);
    rcu_periph_clock_enable(RCU_GPIOC);

    /* configure button pin as input */
    gpio_init(UP_KEY_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, UP_KEY_PIN);

    /* enable and set key EXTI interrupt to the lowest priority */
    nvic_irq_enable(UP_KEY_EXTI_IRQn, 1U, 0U);

    /* connect key EXTI line to key GPIO pin */
    gpio_exti_source_select(UP_KEY_EXTI_PORT_SOURCE, UP_KEY_EXTI_PIN_SOURCE);

    /* configure key EXTI line */
    exti_init(UP_KEY_EXTI_LINE, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(UP_KEY_EXTI_LINE);


    /* DOWN */
    rcu_periph_clock_enable(DOWN_KEY_GPIO_CLK);
    rcu_periph_clock_enable(RCU_GPIOB);

    /* configure button pin as input */
    gpio_init(DOWN_KEY_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, DOWN_KEY_PIN);

    /* enable and set key EXTI interrupt to the lowest priority */
    nvic_irq_enable(DOWN_KEY_EXTI_IRQn, 3U, 0U);

    /* connect key EXTI line to key GPIO pin */
    gpio_exti_source_select(DOWN_KEY_EXTI_PORT_SOURCE, DOWN_KEY_EXTI_PIN_SOURCE);

    /* configure key EXTI line */
    exti_init(DOWN_KEY_EXTI_LINE, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(DOWN_KEY_EXTI_LINE);

    return;
}
/**
 * @func: 
 * @description: 确认键
 * @return {*}
 * @example: 
 */
static void confirm_button_init(void)
{
     /* enable the key clock */
     rcu_periph_clock_enable(CONFIRM_KEY_GPIO_CLK);
     rcu_periph_clock_enable(RCU_AF);
 
     /* configure button pin as input */
     gpio_init(CONFIRM_KEY_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, CONFIRM_KEY_PIN);
 
     /* enable and set key EXTI interrupt to the lowest priority */
     nvic_irq_enable(CONFIRM_KEY_EXTI_IRQn, 4U, 0U);
 
     /* connect key EXTI line to key GPIO pin */
     gpio_exti_source_select(CONFIRM_KEY_EXTI_PORT_SOURCE, CONFIRM_KEY_EXTI_PIN_SOURCE);
 
     /* configure key EXTI line */
     exti_init(CONFIRM_KEY_EXTI_LINE, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
     exti_interrupt_flag_clear(CONFIRM_KEY_EXTI_LINE);

     return;
}